using System.Diagnostics;

using FineCollection.Test;
using FineCollection.Simplex;

using NUnit.Framework;

using System.Linq;



namespace FineCollection.Const
{
    [TestFixture]
    public class ConstSortMap_test
    {


        [Test]
        public void Test_IndexOf_basic()
        {
            var map = MakeMap("aaaa:1111", "bbbb:2222", "cccc:3333");

            int x1 = map.IndexOf("aaaa");
            int x2 = map.IndexOf("bbbb");
            int x3 = map.IndexOf("cccc");

            x1    ._equals_(map.FirstIndex);
            x2    ._equals_(map.FirstIndex+1);
            x3    ._equals_(map.LastIndex);
        }


        [Test]
        public void Test_IndexOf_emprtymap()
        {
            var map = new int[0].ToSortMap(x => 0, x => 0);

            int xxx = map.IndexOf(666);

            xxx   ._equals_(int.MinValue);
        }


        [Test]
        public void Test_ContainsKey_basic()
        {
            var map = MakeMap("aaaa:1111", "bbbb:2222", "cccc:3333");

            map.ContainsKey("aaaa")   ._true_();
            map.ContainsKey("bbbb")   ._true_();
            map.ContainsKey("cccc")   ._true_();

            map.ContainsKey("dddd")   ._false_();
            map.ContainsKey(null)     ._false_();
        }


        [Test]
        public void Test_ContainsKey_empty()
        {
            var map = MakeMap();

            map.ContainsKey("aaaa")   ._false_();
            map.ContainsKey(null)     ._false_();
        }


        private static ConstSortMap<string, string> MakeMap(params string[] strings)
        {
            return strings
                .Select(str => str.ToPair())
                .ToSortMap(p => p.A, p => p.B, 7);
        }


    }


    static class LocalStringUtils
    {
        internal static Pair<string> ToPair(this string str)
        {
            int p = str.IndexOf(':');
            Debug.Assert(p >= 0);

            string strA = str.Substring(0, p);
            string strB = str.Substring(p + 1);

            return new Pair<string>(strA, strB);
        }
    }
}